package com.dawn.cloud.pos.drive.sdk;

import com.dawn.cloud.pos.drive.utils.Constants;
import com.orhanobut.logger.Logger;

import java.util.HashMap;
import java.util.Map;

import cn.koolcloud.jni.ContactlessEvent;
import cn.koolcloud.jni.ContactlessInterface;

/**
 * @author yyh
 *
 * @date 2018/04/13
 *
 * @description 非接触式卡片SDK
 */
public class CLSdk {

    //就诊卡序列号
    private static String card_serial_no = "";

    /**
     * 打开非接触式读卡器
     *
     * @return 返回 ret小于0则打开失败
     */
    public static int open(){
        Map map = new HashMap();
        int ret = ContactlessInterface.open();
        if (ret < 0) {
            Logger.d( "contactLess card open failed");
        } else {
            Logger.d( "contactLess card open success");
        }
        map.put(Constants.MAP_KEY_CODE,ret);
        return ret;
    }

    /**
     * 寻卡操作
     *
     * @return 返回 ret小于0则打开失败
     */
    public static int searchTargetBegin(){
        Map map = new HashMap();
        int ret = ContactlessInterface.searchTargetBegin(
                ContactlessInterface.CONTACTLESS_CARD_MODE_AUTO, 1, -1);
        if (ret >= 0) {
            Logger.d( "contactLess card searchTargetBegin success");
        } else {
            Logger.d( "contactLess card searchTargetBegin failed");
        }
        map.put(Constants.MAP_KEY_CODE,ret);
        return ret;
    }

    /**
     * 寻卡 等待设备事件
     *
     * @param event
     *
     * @return返回 ret小于0则打开失败
     */
    public static Map pollEvent(ContactlessEvent event){
        Map map = new HashMap();
        int ret = -1;
        ret = ContactlessInterface.pollEvent(-1, event);
        String data = new String();
        if (ret >= 0) {
            if (event.nEventID == 0 && event.nEventDataLength > 0) {
                for (int i = 0; i < event.nEventDataLength; i++)
                    data += String.format("%02X", event.arryEventData[i]);
                Logger.d( "contactLess card pollEvent success arrayEventData:"+data);
            } else if (event.nEventID == 2) {
                Logger.d( "contactLess card pollEvent failed");
            }
        }
        map.put(Constants.MAP_KEY_CODE,ret);
        map.put(Constants.MAP_KEY_DATA,data);
        close();
        return map;
    }

    /**
     * 读取Mifare卡数据
     *
     * @param nSectorIndex  扇区引索
     *
     * @param nBlockIndex 块引索
     *
     * @return ret小于0则打开失败
     */
    public static Map readMifare(int nSectorIndex, int nBlockIndex){
        Map map = new HashMap();
        byte[] pDataBuffer = new byte[16];
        int result = ContactlessInterface.readMifare(nSectorIndex, nBlockIndex,
                pDataBuffer, pDataBuffer.length);
        String temp = new String();
        if (result >= 0) {
            for (int i = 0; i < pDataBuffer.length; i++)
                temp += String.format("%02X", pDataBuffer[i]);
        }
        Logger.d( "readMifare temp:"+temp);
        map.put(Constants.MAP_KEY_CODE,result);
        map.put(Constants.MAP_KEY_DATA,temp);
        return map;
    }


    /**
     * 停止检卡
     *
     * @return
     */
    public static Map searchTargetEnd(){
        Map map = new HashMap();
        int ret = ContactlessInterface.searchTargetEnd();
        if (ret >= 0) {
            Logger.d( "contactLess card searchTargetEnd success");
        } else {
            Logger.d( "contactLess card searchTargetEnd failed");
        }
        map.put(Constants.MAP_KEY_CODE,ret);
        return map;
    }

    /**
     * 关闭非接触读卡设备
     *
     * @return
     */
    public static int close(){
        Map map = new HashMap();
        ContactlessInterface.cancelPoll();
        int ret = ContactlessInterface.close();
        if (ret < 0) {
            Logger.d( "contactLess card close failed");
        } else {
            Logger.d( "contactLess card close success");
        }
        map.put(Constants.MAP_KEY_CODE,ret);
        return ret;
    }

    /**
     * 读取就诊卡序列号
     *
     * @return
     */
    public static String readCardSerialNo(){
        int ret = open();
        if(ret>=0){
            ret = searchTargetBegin();
            if(ret>=0){
                ContactlessEvent event = new ContactlessEvent();
                Map map = pollEvent(event);
                card_serial_no = (String) map.get(Constants.MAP_KEY_DATA);
            }
        }
        return card_serial_no;
    }

}
